package com.zhny.aiot.ksd.flow.v2.plugin;

import com.zhny.aiot.ksd.flow.v2.plugin.model.FlowV2StoreEntry;
import com.zhny.aiot.plugin.ICenterFacilities;
import com.zhny.aiot.plugin.IDeviceFactory;
import com.zhny.aiot.plugin.core.model.IRequestMessage;
import com.zhny.aiot.plugin.modbus.ModemEngine;
import com.zhny.aiot.plugin.modbus.device.ModemConfig;
import com.zhny.aiot.plugin.modbus.device.modbus.ModbusNumDevice;
import com.zhny.aiot.plugin.modbus.handler.model.ModemResponseMessage;
import com.zhny.aiot.plugin.model.DeviceEntity;
import com.zhny.aiot.plugin.model.TaskEntity;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;

public class FlowV2Device extends ModbusNumDevice<FlowV2StoreEntry> {
    public FlowV2Device(ModemEngine engine, IDeviceFactory<ModemConfig> factory, ICenterFacilities centerFacilities, DeviceEntity entity) {
        super(engine, factory, centerFacilities, entity);
    }

    @Override
    public FlowV2StoreEntry decode(ModemResponseMessage msg) {
//        8A 19 C0 C9 C0 85 BD CA 60 5B BC CE 00 00 00 00 12 50 00 00 7C 9B 3F 6C
//        5c 11 c1 06 2b 04 be 07 95 92 bd 09 00 00 00 00 12 50 00 00 7c 9b 3f 6c
//        92 fb c1 12 74 be be 13 17 88 bd 16 00 00 00 00 12 50 00 00 7c 9b 3f 6c
        logger.info ("ksd-flow-hex {}", ByteBufUtil.hexDump (msg.getData ()));
        ByteBuf buf = msg.getData();
        Float flow1=convertFloat(buf);
        Float flowSpeed1=convertFloat(buf);
        Float percentage1=buf.readFloat();
        Float flowPercentage1=buf.readFloat();
//        int totalFlowHig1 = buf.readIntLE ();

        byte b0 = buf.readByte();
        byte b1 = buf.readByte();
        byte b2 = buf.readByte();
        byte b3 = buf.readByte();

        int totalFlowHig1 = (b2 << 24) & 0xff000000 |
                (b3 << 16) & 0x00ff0000 |
                (b0 << 8)  & 0x0000ff00 |
                (b1)       & 0x000000ff;
//        int totalFlowHig1=((buf.readByte() << 8) &0x0000ff00|
//                (buf.readByte()) &0x000000ff|
//                (buf.readByte() << 16) &0x00ff0000 |
//                (buf.readByte() << 24) & 0xff000000);
        Float totalFlowLo1=convertFloat(buf);
//        Float totalFlow = totalFlowHig1 + 0.0F;
        Float totalFlow = totalFlowHig1 + totalFlowLo1;
        FlowV2StoreEntry entry = new FlowV2StoreEntry();
        entry.setFlow(String.format ("%.2f",flow1));
        entry.setFlowSpeed(String.format ("%.2f",flowSpeed1));
        entry.setTotalFlow( String.format ("%.2f",totalFlow));
        logger.info ("ksd-flow[{}]",entry);
        return entry;
    }


    private float convertFloat(ByteBuf buf){
        byte b0 = buf.readByte();
        byte b1 = buf.readByte();
        byte b2 = buf.readByte();
        byte b3 = buf.readByte();

        int value = (b2 << 24) & 0xff000000 |
                (b3 << 16) & 0x00ff0000 |
                (b0 << 8)  & 0x0000ff00 |
                (b1)       & 0x000000ff;
        return Float.intBitsToFloat (value & 0x7fffffff);
    }
    @Override
    public boolean checkError(ModemResponseMessage modemResponseMessage) {
        return modemResponseMessage.getData ().readableBytes () == 24;
    }

    @Override
    protected IRequestMessage getCommand(TaskEntity taskEntity) {
        return getFactory ().getRequestMessage (getConfig ());
    }

    @Override
    protected void disposable() {

    }
}
